package com.chen.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.Result;
import com.chen.mapper.OrderMapper;
import com.chen.mapper.PointMapper;
import com.chen.mapper.UserMapper;
import com.chen.pojo.Order;
import com.chen.pojo.Point;
import com.chen.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * @author chen
 * @date 2022/5/9 14:13
 */

@RestController
@RequestMapping("/order")
@Api(value="OrderController",tags="订单")
public class OrderController {


    @Resource
    private HttpServletRequest request;

    @Resource
    OrderMapper orderMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    PointMapper pointMapper;

    public static Integer id1;

    @GetMapping("/buy/{id}")
    @ApiOperation("支付宝请求")
    public Result<?> buy(@PathVariable Long id) {
        String userId = request.getHeader("X-ID");
        Point point = pointMapper.selectById(id);
        String orderNo = IdUtil.randomUUID();
        String payUrl = "http://localhost:8081/alipay/pay?subject=" + point.getPoint() + "&traceNo=" + orderNo+ "&totalAmount=" + point.getMoney()+"&totalPoints="+point.getPoint();


        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getId, Integer.parseInt(userId)));


        Order order = new Order();
        order.setOrderNo(orderNo);
        order.setTotalPrice(point.getPoint());
        order.setPayPrice(point.getMoney());

        order.setUserId(user.getId());
        id1 = user.getId();
        order.setUsername(user.getUsername());
        order.setName(point.getName());
        save(order);
//        orderMapper.insert(order);
        // 新建订单，扣减库存
//        orderMapper.insertOrderTemplate(order);

        String token = request.getHeader("token");
        System.out.println(token);

        return Result.success(payUrl);
    }

//插入
    @PostMapping
    @ApiOperation("插入")
    public Result<?> save(@RequestBody Order order) {
        orderMapper.insert(order);
        return Result.success();
    }


    @GetMapping
    @ApiOperation("分页")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Order::getOrderNo, search);
        }
        Page<Order> OrderPage = orderMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(OrderPage);
    }


    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public Result<?> delete(@PathVariable Long id) {
        orderMapper.deleteById(id);
        return Result.success();
    }

}
